//@ sourceURL=promoteList.js
var tbPrompt = $('#promptlist'),
fmPrompt=$('#fmPrompt');

$(function() {
    tbPrompt.datagrid({
        url: nconPromptList + '/getPrompt',
        fit: true,
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        pageSize: 20,
        pageList: [20, 30, 40],
        pagerNumber: 1,
        singleSelect: true,
        columns: [
            [{
                field: 'id',
                title: 'ID',
                // checkbox:true,
                hidden: true
            }, {
                field: 'cardno',
                title: '卡号',
                width: 50
            },  {
                field: 'amount',
                title: '金额',
                width: 50
            }, {
                field: 'mark',
                title: '备注',
                width: 100
            }]
        ],
        toolbar: '#tbPrompt',
    });
});

prompt = {
    opurl:'',
    add:function(){
        this.openFm();
        fmPrompt.form('clear');
        $('#txtMark').val('此卡可全额抵扣桃李软件就业班课程学费；\r\n此卡不记名，不挂失，不与其他同类优惠叠加；');
        this.opurl = nconPromptList + '/addPrompt';
    },
    edit: function() {
        var row = tbPrompt.datagrid('getSelected');
        if (row) {
            fmPrompt.form('load', row);
            this.openFm();
            this.opurl = nconPromptList + '/updatePrompt';
        } else {
            $.messager.alert('警告', '请先选择一行要修改的数据！');
        }
    },
    save: function() {
        // $.messager.progress({
        //     text: '正在处理...',
        // });
        var us = fmPrompt.form('serialize');
        // console.log(us);
        // return;

        var url = this.opurl;
        console.log(url);

        fmPrompt.ajaxSubmit({
            type: 'post',
            url: url,
            success: function(data) {
                console.log(data);
                if (data > 0) {
                    $.messager.show({
                        title: '成功',
                        msg: '保存成功！！！',
                        timeout: 1000,
                        showType: 'slide'
                    });
                    fmPrompt.dialog('close');
                    tbPrompt.datagrid('reload');
                }else if(data==-1){
                    $.messager.show({
                        title: 'Error',
                        msg: '已经存在该卡号！！！'
                    });
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: '保存失败！！！'
                    });
                }
            },
            error: function(XmlHttpRequest, textStatus, errorThrown) {
                console.log(XmlHttpRequest);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
    },
    del: function() {
        var row = tbPrompt.datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认框', '确定要删除该条数据吗?', function(r) {
                if (r) {
                    $.post(nconPromptList + '/deletePrompt/', { id: row.id }, function(data) {
                        if (data > 0) {
                            $.messager.show({
                                title: '删除成功',
                                msg: '删除成功！！！',
                                timeout: 1000,
                                showType: 'slide'
                            });
                            tbPrompt.datagrid('reload'); // reload the user data
                        } else {
                            $.messager.show({ // show error message
                                title: 'Error',
                                msg: '删除失败！！！'
                            });
                        }
                    });
                }
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '还未选择数据，请选择！！！',
                timeout: 1000,
                showType: 'slide'
            });
        }
    },
    search: function() {
        tbPrompt.datagrid('load', {
            no: $.trim($('#txtPromptNo').val())
        });
    },
    reset: function() {
        $('#txtPromptNo').val('');
        tbPrompt.datagrid('load', {});
    },
    openFm: function() {
        fmPrompt.dialog({
            width: 350,
            title: '优惠卡 管理',
            iconCls: 'icon-add',
            modal: true,
            closed: true,
            buttons: [{
                text: '保存',
                iconCls: 'icon-save',
                handler: function() {
                    prompt.save();
                }
            }, {
                text: '取消',
                iconCls: 'icon-redo',
                handler: function() {
                    fmPrompt.dialog('close');
                }
            }]
        }).dialog('open');
    }
};


$(document).keydown(function(e) {
    if (e.keyCode == 13) {
        prompt.search();
    }
});
